package level.editor.menu;

import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;

import core.Constants;

import level.editor.command.Command;
import level.editor.state.UpdateMenuCommand;

/*
 * Implementation of the file menu category. Planned functions include: New,
 * Save, Save as, Open, Exit. Actual implementation of these commands will
 * happen in the StateModule.
 */

public class MenuFile extends MenuCategory
{
	private JMenuItem newButton;
	private JMenuItem openButton;
	private JMenuItem saveButton;
	private JMenuItem saveAsButton;

	public MenuFile(MenuView view)
	{
		super("File", view);
		setMnemonic(KeyEvent.VK_F);
		getAccessibleContext().setAccessibleDescription(
				"This is the main file menu");
	}

	@Override
	protected void init()
	{
		String iconDir = Constants.ICON_DIR;
		
		newButton = new JMenuItem("New...", new ImageIcon(iconDir + "/new.png"));
		newButton.setActionCommand("NEW");
		newButton.addActionListener(view);
		add(newButton);

		openButton = new JMenuItem("Open...", new ImageIcon(iconDir
				+ "/open.png"));
		openButton.setActionCommand("OPEN");
		openButton.addActionListener(view);
		add(openButton);

		saveButton = new JMenuItem("Save", new ImageIcon(iconDir + "/save.png"));
		saveButton.setActionCommand("SAVE");
		saveButton.addActionListener(view);
		saveButton.setEnabled(false);
		add(saveButton);

		saveAsButton = new JMenuItem("Save As...");
		saveAsButton.setActionCommand("SAVEAS");
		saveAsButton.addActionListener(view);
		saveAsButton.setEnabled(false);
		add(saveAsButton);
	}

	@Override
	protected void pushDownCommand(Command command)
	{
		if (command instanceof UpdateMenuCommand)
		{
			if (saveButton != null)
			{
				saveButton.setEnabled(((UpdateMenuCommand) command)
						.isModified());
			}

			if (saveAsButton != null)
			{
				saveAsButton.setEnabled(((UpdateMenuCommand) command)
						.isModified());
			}
		}
	}
}
